/*
* @author suhong
* @date 2022/5/14 22:10
* @file index.js
* @说明  路由配置文件
*/
import Login from  '../components/Login'
import VueRouter from "vue-router"
import Register from "@/components/Register";
import Home from "@/components/Home";
import NewContent from "@/components/NewContent";
import MyBlog from "@/components/MyBlog";
import EditUser from "@/components/EditUser";
import WriteArticle from "@/components/WriteArticle";
import Author from "@/components/Author";
import HomePage from "@/view/layout/HomePage";
import Index from "@/view/home/Index";
import UserManagement from "@/view/UserRegulate/UserManagement";
import VaC from "@/view/test/VaC";

const router=  new VueRouter({
    scrollBehavior(){
      return{x:0,y:0}  
    },
    routes: [
        {
            path: '/',
            redirect: '/login' //重定向到登录页面
        },
        {
            //登录路由
            name: 'login',
            path: '/login',
            component: Login
        },
        {
            //注册路由
            name: 'register',
            path: '/register',
            component: Register
        },
        {
            //首页路由
            name: 'home',
            path: '/home',
            component: Home
        },
        {
            //文章详情路由
            name: 'newContent',
            path: '/news',
            component: NewContent
        },
        {
            //个人中心
            name: 'myBlog',
            path: '/myinfo',
            component: MyBlog
        },
        {
            //用户编辑页面
            name: 'editUser',
            path: '/userEdit',
            component: EditUser
        },
        //创作页面
        {
            name: 'WriteArticle',
            path: '/write',
            component: WriteArticle
        },
        //某个作家的详情页
        {
            name:'author',
            path:'/authors',
            component:Author
        },
        //后台管理页面
        {
            name:'adminHome',
            path:'/admin/home',
            redirect:'/admin/home/index',
            component:HomePage,
            children:[
                {
                    name:'index',
                    path:'index',
                    component:Index
                },
                //用户管理页面
                {
                    name:'UserManagement',
                    path:'user',
                    component:UserManagement
                },
                {
                    name:'test',
                    path:'test',
                    component:VaC
                }
            ]
        }
    ]
})
router.beforeEach((to,from,next)=>{
    
    if(to.name==='login' || to.name==='register'){
        next()
    }else{
        if(localStorage.getItem('token')){
            next()
        }else {
            next('/login')
        }
    }
})
router.afterEach(()=>{
   window.scrollTo(0,0)
 
   
})
export  default router